package com.alawn.framework.core.web.config;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import javax.faces.webapp.FacesServlet;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;

import org.ocpsoft.rewrite.servlet.RewriteFilter;
import org.primefaces.webapp.filter.FileUploadFilter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import com.alawn.framework.core.web.faces.event.JsfConfigureListener;

@Configuration
public class WebConfig implements ServletContextAware {

	@Override
	public void setServletContext(ServletContext servletContext) {
		servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
		servletContext.setInitParameter("javax.faces.FACELETS_SKIP_COMMENTS", "true");
		servletContext.setInitParameter("javax.faces.STATE_SAVING_METHOD", "server");
		servletContext.setInitParameter("javax.faces.PROJECT_STAGE", "Development");
		servletContext.setInitParameter("primefaces.THEME", "ecuador-indigo");
		servletContext.setInitParameter("primefaces.FONT_AWESOME", "true");
		servletContext.setInitParameter("primefaces.CLIENT_SIDE_VALIDATION", "true");
		servletContext.setInitParameter("primefaces.UPLOADER", "auto");
		servletContext.setInitParameter("org.ocpsoft.rewrite.config.CONFIG_RELOADING", "true");
		// 必须要比shiroFilter加载后，不然是shiro取不了权限
//		servletContext.addFilter("rewrite", new RewriteFilter()).addMappingForUrlPatterns(
//				EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR),
//				true, "/*");

	}

	@Bean
	@Qualifier("facesServlet")
	public ServletRegistrationBean<FacesServlet> facesServletBean() {
		ServletRegistrationBean<FacesServlet> servletBean = new ServletRegistrationBean<>(new FacesServlet(),
				"*.faces");
		servletBean.setLoadOnStartup(1);
		servletBean.setName("Faces Servlet");
		System.out.println("=====Faces Servlet");
		return servletBean;
	}

	@Bean
	public ServletListenerRegistrationBean<JsfConfigureListener> jsfConfigureListenerBean() {
		System.out.println("=====Jsf Configure Listener");
		return new ServletListenerRegistrationBean<>(new JsfConfigureListener());
	}

	@Bean
	public FilterRegistrationBean<FileUploadFilter> fileUploadFilter() {
		FilterRegistrationBean<FileUploadFilter> fileUploadFilter = new FilterRegistrationBean<>(
				new FileUploadFilter());
		Map<String, String> initParameters = new HashMap<String, String>();
		initParameters.put("thresholdSize", "5242880");
		initParameters.put("encoding", "UTF-8");
		fileUploadFilter.setInitParameters(initParameters);
		fileUploadFilter.setName("PrimeFaces FileUpload Filter");
		fileUploadFilter.addServletNames("Faces Servlet");
		return fileUploadFilter;
	}

	@Bean
	public FilterRegistrationBean<CharacterEncodingFilter> encodingFilter() {
		FilterRegistrationBean<CharacterEncodingFilter> encodingFilter = new FilterRegistrationBean<>(
				new CharacterEncodingFilter());
		encodingFilter.setName("Character Encoding Filter");
		Map<String, String> initParameters = new HashMap<String, String>();
		initParameters.put("encoding", "UTF-8");
		initParameters.put("forceEncoding", "true");
		encodingFilter.setInitParameters(initParameters);
		return encodingFilter;
	}

	@Bean
	public FilterRegistrationBean rewriteFilter(@Qualifier("facesServlet") ServletRegistrationBean facesServlet) {
		FilterRegistrationBean rewriteFilter = new FilterRegistrationBean(new RewriteFilter(), facesServlet);
		rewriteFilter.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.ASYNC,
				DispatcherType.ERROR);
		rewriteFilter.addUrlPatterns("/*");
		return rewriteFilter;
	}
}
